--[[=========================================================
	LOCALS
=========================================================]]--
local OLD_ANGLES		= Angle( 0, 0, 0 )
local SAVED_ANGLES		= Angle( 0, 0, 0 )
local FREE_VIEW			= false

--[[=========================================================
	FUNCTIONS
=========================================================]]--

--[[-----------------------------------------------------]]--
--	Calculates free view angles
--[[-----------------------------------------------------]]--
local function CalculateFreeView( ply, angles, offset )

	--	Toggle free view
	if ply:KeyDown( IN_WALK ) then		
		if not FREE_VIEW then
			OLD_ANGLES	= angles
			FREE_VIEW	= true
		end		
	else
		if FREE_VIEW then FREE_VIEW	= false end
	end
	
	--	Smoothly get back to original angles
	if not FREE_VIEW then
		SAVED_ANGLES= Angle( SAVED_ANGLES.p - SAVED_ANGLES.p / 10, SAVED_ANGLES.y - SAVED_ANGLES.y / 10, SAVED_ANGLES.r - SAVED_ANGLES.r / 10 )
		
	--	Specific angles
	else
		SAVED_ANGLES= angles - OLD_ANGLES + offset
	end
	
	angles				= angles + SAVED_ANGLES
	
	return angles
	
end

--[[=========================================================
	GAMEMODE EVENTS
=========================================================]]--

--[[-----------------------------------------------------]]--
--	Computes first person view
--[[-----------------------------------------------------]]--
function GM:CalcFirstPersonView( ply, pos, angles, fov, nearZ, farZ )

	--	Add footstep movement
	local angof			= Angle( 0, 0 , 0 )
	if tobool( GetConVarNumber( "sc_footstep_mvt" ) ) then
		local vlen		= ply:GetVelocity():Length()
		local fovp		= 1--( ( 90 / fov ) * 0.45 )
		local footx		= ply.FootX or 0
		local footy		= ply.FootY or 0
		if ply:GetActiveWeapon().GetIronsights then sights = ply:GetActiveWeapon():GetIronsights() end
		if sights then footx = footx / 2 end
		if sights then footy = footy / 2 end
		angof			= ( Angle( ( footx * 1.1 ) * vlen / 1000, ( footx * 1.7 ) * vlen / 1000, ( footx * 2 ) * vlen / 1000 ) ) * fovp
		pos				= pos + ( angles:Right() * ( footx * 1.2 ) + angles:Up() * ( footy * 1.05 ) ) * fovp
		angles			= angles + angof
	end
	
	--local fang			= CalculateFreeView( ply, angles, -angof )

	--	Setup 1st person view
	local view			= {}
	view.origin			= pos
	view.angles			= angles
	view.fov			= fov	
	view.vm_origin = Vector( 0, 0, 0 )
	if FREE_VIEW then
		view.vm_angles		= OLD_ANGLES
	else
		view.vm_angles		= Angle( 0, 0, 0 )
	end
	
	return view

end

--[[-----------------------------------------------------]]--
--	Computes third person view
--[[-----------------------------------------------------]]--
function GM:CalcThirdPersonView( ply, pos, angles, fov, nearZ, farZ )

	if ply.Vehicle and IsValid( ply.Vehicle ) then
		local vView		= {}
		vView.origin	= pos - angles:Forward() * 300 + angles:Up() * 50
		vView.angles	= angles
		vView.fov		= fov
		return vView
	end

	--	Define view offset
	local fovOffset		= 100 / fov
	local xOffset		= -10 - 2 * fovOffset
	local yOffset		= 80 + fovOffset
	local zOffset		= 10 - fovOffset
	local pitchOffset	= 0
	local yawOffset		= 0
	local rollOffset	= 0
	local origin		= pos
	
	--	Offset more when crouching
	if ply:Crouching() then
		xOffset			= -20 - 2 * fovOffset
		yOffset			= 70 + fovOffset
		zOffset			= 30 - fovOffset
	end
	
	--	Offset angles
	angles				= angles + Angle( pitchOffset, yawOffset, rollOffset )
	
	--	Calculate free view
	angles				= CalculateFreeView( ply, angles, Angle( pitchOffset, yawOffset, rollOffset ) )
	
	--	Offset position
	origin				= pos - angles:Forward() * yOffset + angles:Up() * zOffset - angles:Right() * xOffset
	
	--	Setup 3rd person view
	local view			= {}
	view.origin			= origin
	view.angles			= angles
	view.fov			= fov
	
	return view

end